<?php
/**
 * Created by PhpStorm.
 * User: Pico_Dragon
 * Date: 23/12/2014
 * Time: 00:57
 */
session_start();
include "../function/helper.php";
include "../../db/dbConnect.php";
include '../function/pagging.php';

error_reporting(E_ALL && ~ E_NOTICE);
$p=new Pager();
$p->setLimit(20);
$p->findStart($p->getLimit());

if($_SERVER['REQUEST_METHOD'] == 'GET') {
    $keyword = escape($_GET['keyword']);
    $address=escape($_GET['address']);
    $sex=escape($_GET['sex']);
    $ageFrm=escape($_GET['ageFrm']);
    $ageTo=escape($_GET['ageTo']);

    $where="where mt.deleted=0 and mt.permit=0
            and mt.iduser!=".$_SESSION['LoginData']['iduser']." ";
    if($keyword !=''){
        $where.= " and (lstname LIKE N'%" . $keyword . "%'
            or fstName like N'%" . $keyword . "%'
            or email LIKE '%" . $keyword . "%') ";
        $p->setKeySrch($keyword);
    }
    if($address >0){
        $where.=" and jt.id = ".$address." ";
        $p->setKindKey($permit);
    }
    if($sex > 0){
        $where.=" and sex=".$sex." ";
        $p->setSex($sex);
    }
    if($ageFrm && $ageTo){
        $where.=" and (age between ".$ageFrm." and ".$ageTo.") ";
        $p->setAgeFrm($ageFrm);
        $p->setAgeTo($ageTo);
    }else{
        if($ageFrm){
            $where.=" and age >=".$ageFrm." ";
            $p->setAgeFrm($ageFrm);
        }
        if($ageTo){
            $where.=" and age <=".$ageTo." ";
            $p->setAgeTo($ageTo);
        }
    }


    $sql="select mt.*, jt.name as que
          from u_user as mt JOIN que_quan as jt ON mt.address=jt.id ".$where."
          ";

}
$sql2=$sql;
$result=mysql_query($sql);
if(mysql_error()){
    echo mysql_error();
    exit();
}

$count=0;
$count=mysql_num_rows($result);
$p->findPages($count, $p->getLimit());

$start=$p->getStart();
$limit=$p->getLimit();
$sql2.=' limit '.$start.','.$limit.'';

$result=mysql_query($sql2);
if(mysql_error()){
    echo mysql_error();
    exit();
}
?>

<table class="listQ w100p">
    <thead>
    <th class="tl w25p"></th>
    <th class="tc w25p "></th>
    <th class="tc w25p"></th>
    <th class="tc" style="width: 12%"></th>
    </thead>
    <div class="clear"></div>
    <tbody>
    <?php
    $i=0;
    $open=false;

    while($row=mysql_fetch_array($result)) {
        if($i==4){
            if($open){
                $open =false;
                echo "</tr>";
            }elseif(!$open){
                $open=true;
                echo "<tr>";
            }
            $i=0;

        }{
            $avatar=($row['avatar'])?$row['avatar']:"avatar_default.jpg";
            $html= '<td><div class="userItem"><a href="web/member/profile.php?id_profile='.$row['iduser'].'">
                        <img class="avatar" src="web/images/avatar/'.$avatar.'"/>
                        <div class=" w100p userName">'.$row['lstName'].' '.$row['fstName'].'</div>
                        <div class="w100p">'.$row['pref'].'%</div>
                    </a></div></td>';
            echo $html;
        }
        $i++;
    }
    if($i==0){
        echo '<tr>
                        <td colspan="4">Rất tiếc, hiện chưa có kết quả nào phù hợp! </td>
                    </tr>';
    }
    ?>
    <tr>
        <td colspan="4">
            <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#E0FFFF">
                <tr>
                    <td align="center" valign="middle">Từ <?php echo($p->getStart()+1).' đến '.((($end=($p->getStart()+$p->getLimit()))>$count)?$count:$end).' trong tổng cộng '.$count.'';?></td>
                    <td align="center" valign="middle">
                        <?php
                        $p->setLink();
                        $page=$p->pageList($_GET["page"],$p->getPage(),"web/member/friendSearch.php");
                        echo $page;
                        ?></td>
                </tr>
            </table>
        </td>
    </tr>
    </tbody>
</table>

<script type="text/javascript">

    $('a.pag_nav').click(function(){
        var $href=$(this).attr('href');
        //alert($href);
        $(" #tbList").load($href);
        return false;
    });
</script>